#include "build.h"
#include "fig_player_craft.h"
#include "fig_playerbullet_master.h"
#include "fig_bullet.h"
#include "fig_bullet_manager.h"
#include "fighter.h"
#include "fig_enemy_manager.h"
#include "fig_sfx_manager.h"
#include "fig_vars.h"
#include "gen_game_state.h"
#include "fig_vars.h"
#include "fig_enemybullet_master.h"
#include "fig_award_tracker.h"
#include "fig_constants.h"
#include "fig_gamecenter.h"
#include "fig_level.h"
#include "fig_conversation.h"
#include "fig_ui_pausemenu.h"
#include "build.h"

#include "math/facing.h"
#include "num/rand.h"
#include "gameflow/game_flow_manager.h"
#include "application/game_rms.h"
#include "ui/ui_progress.h"
#include "lang/lang.h"
#include "lang/strman.h"
#include "lang/ConvertUTF.h"
#include "font/font_manager.h"

#ifndef PRO_VERSION
#include "admanager/admanager.h"
#endif


#include "util/mmgr.h"				// Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"				// logging
#include "util/timer.h"				// logging

#ifdef _PLATFORM_IOS
#include "AudioToolbox/AudioToolbox.h"
#endif

const float FIGPlayerCraft::POSITION_COUNTER_DURATION = 0.08f;
const float	FIGPlayerCraft::ARMOR_MAX_COUNTER = 15.0f;

FIGPlayerCraft::FIGPlayerCraft(  ) : 
							FIGCraft(),
							m_bIsMouseDown(false), 							
							m_State(STATE_NOTSHOWNYET),
							m_PickupStarCount(0),
							m_LevelCompeleteCounter(0),
							m_pLvlCompleteShadowObj(NULL),
							m_DyingCounter(0),							
							m_AppearCounter(0.0f),
							m_IsShootButtonPressed(false),
							m_bSkipAd(true),
							m_IsOnICadeMove(false)
{
	
	m_MaxLife = m_Life = FIGConstants::PLAYER_HP;
	m_ExplodeSize = 1.5f;
    
	
	m_ShipSpriteInfo.AddSprite("p_l5");
	m_ShipSpriteInfo.AddSprite("p_l4");
	m_ShipSpriteInfo.AddSprite("p_l3");
	m_ShipSpriteInfo.AddSprite("p_l2");
	m_ShipSpriteInfo.AddSprite("p_l1");
	m_ShipSpriteInfo.AddSprite("p_middle");
	m_ShipSpriteInfo.AddSprite("p_r1");
	m_ShipSpriteInfo.AddSprite("p_r2");
	m_ShipSpriteInfo.AddSprite("p_r3");
	m_ShipSpriteInfo.AddSprite("p_r4");
	m_ShipSpriteInfo.AddSprite("p_r5");
	    
	
    
	m_BoasterSprite.Create("jet");	
    
	m_ShipSpriteInfo.m_ShipIndex = ShipSpriteInfo::CRAFT_MIDDLE;	
	m_IsAlive = true;
    
    
	const float Step = 1.0f;
	m_Rotations.push_back( 360 - Step * 5);
	m_Rotations.push_back( 360 - Step * 4);
	m_Rotations.push_back( 360 - Step * 3);
	m_Rotations.push_back( 360 - Step * 2);
	m_Rotations.push_back( 360 - Step);
	m_Rotations.push_back(0);
	m_Rotations.push_back( Step);
	m_Rotations.push_back( Step * 2);
	m_Rotations.push_back( Step * 3);
	m_Rotations.push_back( Step * 4);
	m_Rotations.push_back( Step * 5);
    
	const float Step2 = 1.0f/(float)GetGraphics()->GetScreenWidth();
	m_XAdjustments.push_back( - Step2 * 5);
	m_XAdjustments.push_back( - Step2 * 4);
	m_XAdjustments.push_back( - Step2 * 3);
	m_XAdjustments.push_back( - Step2 * 2);
	m_XAdjustments.push_back( - Step2);
	m_XAdjustments.push_back(0);
	m_XAdjustments.push_back( Step2);
	m_XAdjustments.push_back( Step2 * 2);
	m_XAdjustments.push_back( Step2 * 3);
	m_XAdjustments.push_back( Step2 * 4);
	m_XAdjustments.push_back( Step2 * 5);
    
	
	m_pUIProgress_Health = (ui::UIProgress*)GENGameState::GetGame()->GetWidget("player_health");
	if(m_pUIProgress_Health)
		m_pUIProgress_Health->SetColor(1.0f,1.0f,1.f,0.8f);
    
	m_pUIShipCount = GENGameState::GetGame()->GetWidget("Player_Count");    
	    
	m_GhostModeBlinkCounter.SetLimit(0.1f);
	
    
    
	GetJoystick()->JoystickEnable();
	m_Pos = math::Vec2(0,1.5f);			// offscreen first
	m_pSprite = NULL;
}

FIGPlayerCraft::~FIGPlayerCraft()
{
	if(m_pLvlCompleteShadowObj)
	{
		m_pLvlCompleteShadowObj->Clear();
		UTIL_SAFE_DELETE(m_pLvlCompleteShadowObj);
	}
    
	m_pSprite = NULL; // dun clear this
	
}

FIGPlayerCraft&	FIGPlayerCraft::Instance()
{
	static FIGPlayerCraft	Inst;
	return Inst;
}

void	FIGPlayerCraft::CacheRender()
{
	m_Armor.m_Sprite.Render();
	m_BoasterSprite.Render();

	/*
	for(int i = 0; i < (int) m_Sprites.size(); i++)
	{
		m_Sprites[i]->Render();
		//if(!m_Sprites[i]->HasMask())
		//	m_Sprites[i]->CreateMask();


	}
	*/
}

void	FIGPlayerCraft::Render(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;
    
	//FIGSFXManager::Instance().Spawn(FIGSfx::TRAIL_JET,m_Pos + math::Vec2(0,m_Radius));
    
	if(m_State == STATE_GHOSTMODE_COMEIN ||
       m_State == STATE_GHOSTMODE)
	{
		if(m_GhostModeBlinkCounter.GetProgress() < 0.3f)
			return;
	}

	//another ship copy of itself
	if(m_Guardian.m_bHasGurdian || 
		m_Guardian.m_IsDisappearing)
	{
		m_Guardian.Render(Offset,Scale);
		
	}
    
	m_Armor.Render(Offset, m_Pos, Scale);
    
	RenderShip(	m_Pos, 
				m_ShipSpriteInfo.m_ShipIndex,
				Offset,
				m_Color,
				Scale);
    
	if(m_State == STATE_LEVEL_COMPLETE_GO_UP)
	{
		if(m_pLvlCompleteShadowObj)
			m_pLvlCompleteShadowObj->Render(Offset,Scale,m_pSprite);
	}
    
	
	//m_Test.m_Pos = math::Vec2(0.5f,0.5f) + Offset;
	//m_Test.RenderRotated();
    
}

void	FIGPlayerCraft::RenderShip(	const math::Vec2&	Pos,
									int					ShipIndex,
									const math::Vec2	Offset,
									COLOR				c,													
									float				Scale
									)
{
	//m_Rotation =  m_Rotations[ShipIndex];

	float BosterScale = 1.0f;

	//int ShipIndexDiff = ShipIndex - (int)FIGPlayerCraft::CRAFT_MIDDLE;
	//if( ShipIndexDiff < 0)
	//	ShipIndexDiff *= -1;

	//BosterScale = 1.0f - ( 0.05f *  ShipIndexDiff);


	m_BoasterSprite.m_Pos = Pos + Offset;
	//m_BoasterSprite.m_Pos.x += m_XAdjustments[ShipIndex];
	m_BoasterSprite.m_Rotation = m_Rotation;
	m_BoasterSprite.m_Size = m_BoasterSprite.m_SizeOriginal * Scale;
	m_BoasterSprite.m_Size.x *= BosterScale;

	m_BoasterSprite.m_Color = ColorfRGBA(1.0f,1.0f,1.0f,1.0f);
	m_BoasterSprite.RenderRotated();
    
	math::Vec2 BackupPos = m_Pos;
	COLOR BackupColor = m_Color;
	m_Pos = Pos;
	//m_Pos.x += m_XAdjustments[ShipIndex];
	m_Color = c;
	m_pSprite = m_ShipSpriteInfo.m_Sprites[ShipIndex];	
	FIGCraft::Render(Offset,Scale);
	m_Pos = BackupPos;
	m_Color = BackupColor;

}


void	FIGPlayerCraft::RenderDebug(const math::Vec2& Offset,float Scale)
{
    
	FIGPlayerBulletMaster::Instance().RenderDebug(Offset,Scale);
	
	FIGCraft::RenderDebug(Offset,Scale);	
}

void	FIGPlayerCraft::Tick(float dt)
{
    
	m_bIsMouseDown = false;
	input::Mouse::BUTTON_STATE* pState = input::Mouse::Instance().GetButtonState(input::Mouse::BUTTON_LEFT);
	if(pState->ButtonDown)
		m_bIsMouseDown = true;
    
	FIGPlayerBulletMaster::Instance().Tick(dt);
	FIGConversation::Instance().Tick(dt);
	FIGConversation::Instance().UpdatePlayerPos(m_Pos - 
												math::Vec2(0, m_ShipSpriteInfo.m_Sprites[FIGPlayerCraft::ShipSpriteInfo::CRAFT_MIDDLE]->m_SizeOriginal.y * 0.5f)
												);
	
    Tick_States(dt);
    
	if(!m_IsAlive)
		return;
    
	m_pSprite = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.m_ShipIndex];
	if(m_ToDie)
	{
		Fighter::Instance().ShakeScreen(1.0f);
	}

	FIGCraft::Tick(dt);
	m_BoasterSprite.Tick(dt);	
	m_Armor.Tick(dt);
	m_NuclearBomb.Tick(dt,m_Pos);	 
	    
	if(m_pUIProgress_Health)
		m_pUIProgress_Health->SetProgress(1 - (m_Life/m_MaxLife));
    
	m_pUIShipCount->Tick(dt);
	

	if(m_IsOnICadeMove)
	{
		m_Movement.Tick(dt);
		m_Pos = m_Movement.GetPos();


		// fix position not to over boundaries.
		const math::Vec2 Offset = Fighter::Instance().GetOffset();
		const float Scale = Fighter::Instance().GetScale();
		const float HalfX = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal.x * 0.5f * Scale;
		const float HalfY = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal.y * 0.65f * Scale;   // a little more for jet

		if(m_Pos.x + Offset.x + HalfX > 1)
			m_Pos.x = 1 - HalfX - Offset.x;
		else if(m_Pos.x + Offset.x - HalfX < 0)
			m_Pos.x = HalfX - Offset.x;
	    
		if(m_Pos.y + HalfY > 1)
			m_Pos.y = 1 - HalfY;
		else if(m_Pos.y -  HalfY < 0)
			m_Pos.y = HalfY;

		m_IsOnICadeMove = false;
	}
	
	
}



void	FIGPlayerCraft::Tick_BulletShooting(float dt)
{

#ifdef _TEST_DONOT_SHOOT_
	if( input::Keyboard::Instance().IsKeyDown(KEY_SPACE) == false)
		return;
#endif


	if(input::MOGAJoystick::Instance().IsConnected())
	{
		if( !m_IsShootButtonPressed)
		{
			return;
		}
		else
		{
			//m_IsShootButtonPressed = false;	// reset it.
		}
	}
	

	WEAPON_TYPE WeaponType = (WEAPON_TYPE)(FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR));
	int WeaponPower = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER);

	FIGBullet::SHOOT_STYLES ShootStyle =GetShootStyle();
	//auto bulet firing
#ifndef _PLATFORM_IOS
//	if( input::Keyboard::Instance().IsKeyDown(KEY_SPACE))
#endif
	{
		FIGPlayerBulletMaster::BULLET_TYPES BulletType = FIGPlayerBulletMaster::BULLET_TYPES_BLUE;
        
		if(WeaponType == WEAPON_BULE)
			BulletType = FIGPlayerBulletMaster::BULLET_TYPES_BLUE;
        
		else if(WeaponType == WEAPON_RED)
			BulletType = FIGPlayerBulletMaster::BULLET_TYPES_RED;
        
		if(WeaponType == WEAPON_GREEN)
			BulletType = FIGPlayerBulletMaster::BULLET_TYPES_GREEN;
        

		FIGPlayerBulletMaster::Instance().ShootTick(
                                                    m_Pos ,
                                                    BulletType,
                                                    dt,
                                                    ShootStyle
                                                    );

		
	}
	
    
	ShootStyle =  (FIGBullet::SHOOT_STYLES)WeaponPower;
	// for window testing -------------------
	//rocket firing
	if( input::Keyboard::Instance().IsKeyDown(KEY_R)
       ||
       ( WeaponType == WEAPON_RED && WeaponPower >= 0)
       )
	{
		switch(WeaponPower)
		{
		case 0:	ShootStyle =  FIGBullet::SHOOT_STYLES_ROCKET2;break; 
		case 1: ShootStyle =  FIGBullet::SHOOT_STYLES_ROCKET4;break; 
		case 2: ShootStyle =  FIGBullet::SHOOT_STYLES_ROCKET6;break; 
		case 3:	ShootStyle =  FIGBullet::SHOOT_STYLES_ROCKET6;break; 
		}
		
		FIGPlayerBulletMaster::Instance().ShootTick(
                                                    m_Pos ,
                                                    FIGPlayerBulletMaster::BULLET_TYPES_ROCKET,
                                                    dt,
                                                    ShootStyle
                                                    );
	}
    
	if( input::Keyboard::Instance().IsKeyDown(KEY_T)
       ||
       WeaponType == WEAPON_GREEN
       )
	{
		switch(WeaponPower)
		{
		
		case 0: ShootStyle =  FIGBullet::SHOOT_STYLES_DOUBLE;break; 
		case 1: ShootStyle =  FIGBullet::SHOOT_STYLES_TRIPPLE;break; 
		case 2:	ShootStyle =  FIGBullet::SHOOT_STYLES_RAMBO_4;break; 
		case 3:	ShootStyle =  FIGBullet::SHOOT_STYLES_RAMBO_5;break; 
		}
		
		FIGPlayerBulletMaster::Instance().ShootTick(
                                                    m_Pos ,
                                                    FIGPlayerBulletMaster::BULLET_TYPES_TOPIDO,
                                                    dt,
													ShootStyle
                                                    );
	}
	// for window testing -------------------
}



void	FIGPlayerCraft::Tick_States(float dt)
{
	switch(m_State)
	{
		case STATE_NOTSHOWNYET:
			if(m_AppearCounter > 0)
			{
				m_AppearCounter -= dt;
			}
			else
			{
				m_State = STATE_SPAWN;
#ifndef PRO_VERSION				

				int LevelNum = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
				if(LevelNum > 1 )
				{
#ifdef _BUILD_CHINESE
					if(m_bSkipAd)
					{
						m_bSkipAd = false;
					}
					else
					{
#endif
						AdManager::Instance().RequestFullScreenAd(2);
						FIGUIPauseMenu::Instance().DisableFor(0.5f);
#ifdef _BUILD_CHINESE
					}
#endif
				}
#endif
			}

            break;
		case STATE_SPAWN:
			SpawnNewCraft();

			// load images here by Ticking it.
			for(int i = 0; i < ShipSpriteInfo::CRAFT_SPRITE_COUNT; i++)
				m_ShipSpriteInfo.m_Sprites[i]->Tick(dt);

			
			m_SizePlayer = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal;
			m_Radius = m_SizePlayer.x * 0.5f;
			

			m_State = STATE_GHOSTMODE_COMEIN;
            break;
		case STATE_GHOSTMODE_COMEIN:
			ComingIntoTheScreen(dt);
            break;
		case STATE_GHOSTMODE:
			m_GhostModeCounter -= dt;
			if(m_GhostModeCounter <= 0)
				m_State = STATE_NORMAL;				
            break;
		case STATE_NORMAL:
			if(m_IsAlive)
			{
								
				Tick_BulletShooting(dt);		
				m_Guardian.Tick(dt, m_Pos, m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal);
				m_ShipSpriteInfo.Tick(dt , m_Pos, m_Rotation);	
				GetJoystick()->Tick(dt);
				if(!m_bIsMouseDown)
					m_Tilt.Tick(dt, m_Pos, m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal );
			}
            break;
		case STATE_DYING: 
			if(m_DyingCounter > 0)
			{
				m_DyingCounter -= dt;
				if(m_DyingCounter < 0)
				{

					// report score to gamecenter here
                    
                    int HighScore = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_HIGHSCORE);
                    HighScore += FIGAwardTracker::Instance().GetCurrentLevelScore(); 
					//FIGGameCenter::Instance()->ReportScore(HighScore);
#ifndef PRO_VERSION
					//int LevelNum = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
					//if(LevelNum > 1)
					//{
					//	AdManager::Instance().RequestFullScreenAd(2);
					//	FIGUIPauseMenu::Instance().DisableFor(0.5f);
					//}
#endif
					gameflow::GameFlowManager::Instance().GetTransition().StartTransition("","MainMenu" , "" , NULL);

					ResetToDefault();
				}
			}
            break;
		case STATE_LEVEL_COMPLETE_GO_UP:
			Tick_LevelCompleteGoUp(dt);
            break;
            
	}
    
	if(m_State == STATE_GHOSTMODE_COMEIN ||
       m_State == STATE_GHOSTMODE)
	{
		m_GhostModeBlinkCounter.Tick(dt);
		if(m_GhostModeBlinkCounter.IsReady())
			m_GhostModeBlinkCounter.Reset();
	}
    
	if(m_LevelCompeleteCounter > 0)
	{
		m_LevelCompeleteCounter -= dt;
		if(m_LevelCompeleteCounter <= 0)
		{

			m_State = STATE_LEVEL_COMPLETE_GO_UP;
			m_pLvlCompleteShadowObj = new FIGPlayerShadow( m_ShipSpriteInfo.m_Sprites[FIGPlayerCraft::ShipSpriteInfo::CRAFT_MIDDLE]->m_SizeOriginal.y * Fighter::Instance().GetScale() * 0.9f);

		}
	}
}



void	FIGPlayerCraft::Tick_LevelCompleteGoUp(float dt)
{
	if(m_Pos.y >  -0.3f)
	{
		m_Pos.y -= 1.0f * dt;
		if(m_Guardian.m_bHasGurdian)
			m_Guardian.m_Pos.y -= 1.0f * dt;

		if(m_pLvlCompleteShadowObj)
			m_pLvlCompleteShadowObj->Tick(dt,m_Pos);
		if(m_Pos.y <=  -0.3f)
		{
			m_pLvlCompleteShadowObj->Clear();
			GENGameState::GetGame()->OnLevelCompete();
		}
	}
}

void	FIGPlayerCraft::SpawnNewCraft()
{
	m_Pos.x = 0.5f;
	m_Pos.y = 1.1f;
	m_Life = m_MaxLife;
	m_IsAlive = true;
	m_Speed =  m_MaxSpeed;
	m_IsOnICadeMove = false;
	m_Movement.SetMoveSpeed(m_Speed * 2.0f);
	m_ShipSpriteInfo.Reset(m_Pos);
}

void	FIGPlayerCraft::ComingIntoTheScreen(float dt)
{
	math::Vec2 Dir = math::Vec2(0,-1.0f);
	float Dist = dt * m_Speed;
	m_Pos += ( Dir * Dist);
	m_Tilt.m_PreviousPosition = m_Pos;
	if(m_Pos.y < 1.0f)
	{
		//slow Down
		m_Speed -= (3.0f * dt);
		if(m_Speed < 0.05f)
			m_Speed = 0.05f;
	}
    
	if(m_Pos.y < 0.85f)
	{
		m_State = STATE_GHOSTMODE;
		m_GhostModeCounter = GHOST_MODE_MAX_COUNTER;
	}
    
	
}

void	FIGPlayerCraft::OnMouseDown(const math::Vec2& Pos)
{

}
	
void	FIGPlayerCraft::OnMouseUp(const math::Vec2& Pos)
{
    if( GameRMS::Instance()->IsTiltEnabled())
	{
		FIGPlayerCraft::Instance().MarkGyroscope();
	}
}


void	FIGPlayerCraft::OnMouseMove(const math::Vec2& From, const math::Vec2& To)
{
	//return;
	if(input::MOGAJoystick::Instance().IsConnected())
		return;	

	if(m_State < STATE_GHOSTMODE)
		return;
    
	if(m_State >= STATE_DYING)
		return;
    
	math::Vec2 Dir = ( To - m_Tilt.m_PreviousPosition);
	m_Tilt.MouseMove(From,To);
	
//#ifdef WIN32
    
	if(!m_bIsMouseDown)
		return;
//#endif

	math::Vec2 Offset = Fighter::Instance().GetOffset();
    
	const float Scale = Fighter::Instance().GetScale();
	const float HalfX = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal.x * 0.5f * Scale;
	const float HalfY = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal.y * 0.65f * Scale;   // a little more for jet
    
    
    // not to jump too far
    //if(Dir.x > m_Size.y )
    //    return;
    //if(Dir.y > m_Size.y)
    //    return;
    //if(Dir.x < -m_Size.y)
    //   return;
   // if(Dir.y < -m_Size.y)
   //    return;
    
    if(Dir.x > HalfX)
        Dir.x = HalfX;
    if(Dir.y > HalfY)
        Dir.y = HalfY;
    if(Dir.x < -HalfX)
        Dir.x = -HalfX;
    if(Dir.y < -HalfY)
       Dir.y = -HalfY;
    
	const float LEVEL_WIDTH_RATION = 1.4f;
	m_Pos +=  math::Vec2(Dir.x * LEVEL_WIDTH_RATION,Dir.y);
    
    // fix position not to over boundaries.
    if(m_Pos.x + Offset.x + HalfX > 1)
        m_Pos.x = 1 - HalfX - Offset.x;
    else if(m_Pos.x + Offset.x - HalfX < 0)
        m_Pos.x = HalfX - Offset.x;
    
    if(m_Pos.y + HalfY > 1)
        m_Pos.y = 1 - HalfY;
    else if(m_Pos.y -  HalfY < 0)
        m_Pos.y = HalfY;
    
    
	
	//FIGGameCenter::Instance()->UpdateValue(FIGGameCenter::SUPER_PILOT,false,false);
	
}


void	FIGPlayerCraft::OnICadeMove(const math::Vec2& To)
{
	if(m_State < STATE_GHOSTMODE)
		return;
    
	if(m_State >= STATE_DYING)
		return;
    
	
	math::Vec2 Offset = Fighter::Instance().GetOffset();
    
	const float Scale = Fighter::Instance().GetScale();
	const float HalfX = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal.x * 0.5f * Scale;
	const float HalfY = m_ShipSpriteInfo.m_Sprites[m_ShipSpriteInfo.CRAFT_MIDDLE]->m_SizeOriginal.y * 0.65f * Scale;   // a little more for jet
    
   
	m_Movement.SetCurrent(m_Pos);
	m_IsOnICadeMove = true;	
	m_Movement.SetMoveSpeed(m_Speed);
	m_Movement.SetTarget(math::Facing::AngleVector(To));	
	//math::Vec2 Dir = To * 0.01f;

      
	//const float LEVEL_WIDTH_RATION = 1.4f;
	//m_Pos +=  math::Vec2(Dir.x * LEVEL_WIDTH_RATION,Dir.y);
    
    
}

void	FIGPlayerCraft::OnControllerBomb()
{
	if(m_State == STATE_NORMAL)
		m_NuclearBomb.OnButtonBomb(ui::UIWidget::WE_LMB_CLICK,NULL);
}

void	FIGPlayerCraft::OnControllerGuardian()
{
	if(m_State == STATE_NORMAL)
		m_Guardian.OnButtonClick(ui::UIWidget::WE_LMB_CLICK,NULL);
}


void	FIGPlayerCraft::UpgradeWeapon(WEAPON_TYPE Type)
{
	int WeaponPower = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER);
	WEAPON_TYPE WeaponType = (WEAPON_TYPE)(FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR));

	if(Type != WeaponType)
	{
		WeaponType = Type;
		FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER, (int)WeaponPower);
		FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR,(int) WeaponType);
		return;			// only upgrade/advance for same weapon type
	}
    
	if(WeaponPower < WEAPON_MAX_POWER )
		WeaponPower++;

	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER, (int)WeaponPower);
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR,(int) WeaponType);
}

void	FIGPlayerCraft::SwitchWeapon()
{
	WEAPON_TYPE WeaponType = (WEAPON_TYPE)(FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR));
	int Temp = (int)WeaponType;
	Temp++;
	if(Temp >= WEAPON_C)
		Temp = 0;

	WeaponType = (WEAPON_TYPE)Temp;   
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR,(int) WeaponType);		
	
}

FIGBullet::SHOOT_STYLES		FIGPlayerCraft::GetShootStyle()
{
	int WeaponPower = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER);
	WEAPON_TYPE WeaponType = (WEAPON_TYPE)(FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR));

	if(WeaponType == WEAPON_GREEN) 
	{
		return FIGBullet::SHOOT_STYLES_DOUBLE;
	}
	else if(WeaponType == WEAPON_RED)
	{
		if(WeaponPower >= 0 && WeaponPower < WEAPON_MAX_POWER)
			return FIGBullet::SHOOT_STYLES_DOUBLE;
		else if(WeaponPower == WEAPON_MAX_POWER) 
			return FIGBullet::SHOOT_STYLES_TRIPPLE_STRAIGHT;
        
	}	
    
	switch(WeaponPower)
	{
		case 0:
			return FIGBullet::SHOOT_STYLES_TRIPPLE;
            break;
		case 1: 
			return FIGBullet::SHOOT_STYLES_RAMBO_4;
            break;
		case 2:
			return FIGBullet::SHOOT_STYLES_RAMBO_5;
            break;
		case 3:
			return FIGBullet::SHOOT_STYLES_RAMBO_6;
            break;
		
            
	}
    
	return FIGBullet::SHOOT_STYLES_DOUBLE;
}


void		FIGPlayerCraft::GotDamage(float Amount,FIGBullet* pBullet, bool Force)
{

#ifdef _TEST_PLAYER_NODAMAGE_
	return;
#endif

	if(m_Life < 0)
		return;

	if(m_Armor.IsActivating())
		return;
		
	FIGCraft::GotDamage(Amount,pBullet,Force);
	
}



void		FIGPlayerCraft::IncreaseBombs()
{
	int BombCount = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_BOMB_COUNT);
	BombCount++;
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_BOMB_COUNT,BombCount);
    
	m_NuclearBomb.ShowCount();
    
}



void	FIGPlayerCraft::OnDie()
{
	FIGCraft::OnDie();
    
	m_Guardian.ForceFinish();

	int WeaponPower = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER);	
	WeaponPower--;
	if(WeaponPower < 0)
		WeaponPower = 0;
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER, (int)WeaponPower);
	
	

	int RemainCount = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_PLAYERCRAFT_REMAIN);
	RemainCount--;
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_PLAYERCRAFT_REMAIN,RemainCount);
    
	if(RemainCount > 0)
	{
		m_State = STATE_NOTSHOWNYET;
		m_AppearCounter = 1.2f;
	}
	else
	{
		m_State = STATE_DYING;
		m_DyingCounter = 5.0f;
	}
    
	ShowShipCount();
	//FIGGameCenter::Instance()->UpdateValue(FIGGameCenter::BOSS_KILLER,false,false);
	FIGSFXManager::Instance().Spawn(FIGSfx::EXPLODING_PLAYER, 
									m_Pos, 
									2.0f)->SetDelay(0.25f);
    
#ifdef _PLATFORM_IOS
	if(GameRMS::Instance()->IsVibrateEnabled() )
        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);    
#endif
	
}

bool	FIGPlayerCraft::CanHit() const
{
	if(m_State == STATE_NORMAL)
		return true;
    
	return false;
}

void	FIGPlayerCraft::PickupRocketShootToAllDir()
{
	FIGPlayerBulletMaster::Instance().ShootRocketAllDir(m_Pos);
}

void	FIGPlayerCraft::OnLevelComplete()
{
	// give sometimes to eat pickups
	// go up
	// show level complete popup
	m_LevelCompeleteCounter = 4.0f;
	
}

void	FIGPlayerCraft::Pickup1Up()
{
	int RemainCount = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_PLAYERCRAFT_REMAIN);
	RemainCount++;
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_PLAYERCRAFT_REMAIN,RemainCount);

	ShowShipCount();
}

void	FIGPlayerCraft::OnJoystickTilt(const math::Vec3& AxisState)
{
	m_Tilt.OnJoystickTilt(AxisState);   
    
}


void	FIGPlayerCraft::StartNewLevel()
{
	m_LevelCompeleteCounter = 0;
	m_State = STATE_NOTSHOWNYET;
	m_AppearCounter = 0.5f;
	m_Life = m_MaxLife;
	m_ShipSpriteInfo.Reset(m_Pos);
	if( GameRMS::Instance()->IsTiltEnabled())
	{
		input::Joystick::Instance().JoystickEnable();
		m_Tilt.m_ToGetOrigin = true;
	}
	else
	{
        input::Joystick::Instance().JoystickDisable();
	}
	
	
	m_NuclearBomb.ShowCount();
	ShowShipCount();
	

	m_NuclearBomb.Reset();

	// offscreen
	m_Pos = math::Vec2(0.5f,1.5f);
	
	//FIGGameCenter::Instance()->UpdateValue(FIGGameCenter::SUPER_PILOT,true,false);
	
	int LevelNum = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
	

	m_Guardian.ForceFinish();
	m_Guardian.m_bHasGurdian = false;


	//int TutorialFinish = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_TUTORIAL_4);
	if(LevelNum == FIGConstants::LASER_SHOOTER_TUTORIAL_LEVEL 		
		)
	{
		//m_Guardian.m_bHasGurdian = true;
		m_Guardian.m_CoolDown.SetLimit(1.0f);
		m_Guardian.m_IsShowingTutorialArrow = true;
		if(m_Guardian.m_pUITutorialArrow)
			m_Guardian.m_pUITutorialArrow->SetIsVisible(true);
	}
	else
	{
		m_Guardian.m_CoolDown.SetLimit((float)FIGConstants::GUARDIAN_ANGEL_COOLDOWN);		
	}

	m_Guardian.m_CoolDown.Reset();	    

	/*
	// random space particles ^_^
	math::Vec2 Pos; 	         	
    const int SPACE_DUST_C = 3;
	for( int i = 0; i < SPACE_DUST_C; i++)
	{
		Pos = math::Vec2( num::RAND32::Instance().RandFloat(0,1.0f),		 
						  num::RAND32::Instance().RandFloat(0,1.0f)
						  );
		FIGSFXManager::Instance().Spawn(
									FIGSfx::SPACE_DUST, 
									Pos,
									Fighter::Instance().GetScale()
									);
	}
	*/
	
	
}

void	FIGPlayerCraft::ShowShipCount()
{
	int Count = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_PLAYERCRAFT_REMAIN);
	if(m_pUIShipCount)
	{
		if(Count > 0)
			m_pUIShipCount->SetTextA(" x" + lang::Lang::FormatNumber(Count));
		else
			m_pUIShipCount->SetTextA("");

	}
	

}


void	FIGPlayerCraft::IncreaseSpecialPowerCoolDown()
{
	m_Guardian.IncreaseCoolDown();
}

void	FIGPlayerCraft::ResetToDefault()
{
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_PLAYERCRAFT_REMAIN, 
								FIGConstants::PLAYER_NEWGIVEN_COUNT);

	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_BOMB_COUNT, 
								FIGConstants::NUCLEAR_NEWGIVEN_COUNT);

	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_POWER, 
								0);

	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_CURRENT_GUN_COLOR, 
		(int)FIGPlayerCraft::WEAPON_BULE);
}

const math::Vec2&	FIGPlayerCraft::GetSize()
{
	return m_SizePlayer;
}
